<?php

namespace app\controller\admin;

use app\common\model\Admin;
use app\common\model\AreaCode;
use app\common\service\AdminService;
use app\common\service\AdminService as Service;
use app\common\util\Result;
use think\annotation\route\Group;
use think\annotation\route\Post;
use think\annotation\route\Route;

/**
 * 管理员 控制器类
 */
#[Group('admin/admin')]
class AdminController extends AdminAuthorizedController
{
    //权限前缀
    protected string $power_prefix = "Admin";

    /**
     * 初始化业务
     * @return Service
     */
    protected function initService()
    {
        return new Service($this->app);
    }

    #[Route("GET", "init")]
    public function initData()
    {

        $res = [
            "areaList" => []
        ];
        return Result::success($res);
    }

    #[Route("GET", "detail")]
    public function detail(Admin $admin)
    {
        $this->checkPower($admin, $this->power_prefix . '_' . 'index');

        return AdminService::instance()->detail(request()->param());
    }

    /**
     * 后台分页
     * @param Admin $admin
     * @return \think\Paginator
     */
    #[Post('page')]
    public function page(Admin $admin)
    {
        $this->checkPower($admin, $this->power_prefix . '_' . 'index');
        return $this->initService()->adminPage();
    }

    /**
     * 创建
     * @param Admin $admin
     * @return Admin|\think\Model
     * @throws \app\common\exception\CatchException
     */
    #[Post('create')]
    public function create(Admin $admin)
    {
        $this->checkPower($admin, $this->power_prefix . '_' . 'create');
        return $this->initService()->create();
    }

    /**
     * 更新
     * @param Admin $admin
     * @return \app\common\model\AdminRole
     * @throws \Exception
     */
    #[Post('update')]
    public function update(Admin $admin)
    {
        $this->checkPower($admin, $this->power_prefix . '_' . 'update');
        return $this->initService()->update();
    }

    /**
     * 删除
     * @param Admin $admin
     * @return \app\common\model\AdminRole|mixed
     * @throws \Exception
     */
    #[Post('delete')]
    public function delete(Admin $admin)
    {
        $this->checkPower($admin, $this->power_prefix . '_' . 'delete');
        return $this->initService()->delete();
    }

    /**
     * 重置密码
     * @param Admin $admin
     * @return \app\common\model\AdminRole|mixed
     * @throws \Exception
     */
    #[Post('resetPassword')]
    public function resetPassword(Admin $admin)
    {
        $this->checkPower($admin, $this->power_prefix . '_' . 'resetPassword');
        return $this->initService()->resetPassword();
    }
}
